;
(function() {
  'use strict';
  angular.module('controllers.userBehaviorAnalysis.srPerceptionUwrAnalysis')
    .controller('srPerceptionRuqAnalysisCtrl', srPerceptionRuqAnalysisCtrl);

  function srPerceptionRuqAnalysisCtrl($scope, srPerceptionRuqAnalysisService, COREDATA, CoreLoading, srPerceptionUwrAlSevice, CoreService) {
    $scope.dimensions = srPerceptionRuqAnalysisService.getDimensions();
    $scope.flag = {
      state: true,
      arrow: false,
      show: 'view',
      showPie: false
    };

    //导出路径
    $scope.formPrefix = COREDATA.FORMPREFIX + 'analysisUserBehavior/apperceiveChurnRate/ChurnNumExport';
    //页面查询项绑定对象
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate() - 61;
    //页面查询项绑定对象
    $scope.params = {
      startTime: new Date(year, month, day),
      endTime: new Date(year, month, day),
      dimension: 't' //维度查询：t:时间、p:省份、b:业务线
    };
    var myEcharts = echarts.init(document.getElementById('doc'));
    var myEcharts1 = echarts.init(document.getElementById('doc1'));
    var myEcharts2 = echarts.init(document.getElementById('doc2'));
    //查询
    $scope.search = function() {
      if (!$scope.flag.state) {
        $scope.flag.state = true;
        return;
      }
      var endTime = $scope.params.endTime.getTime();
      var startTime = $scope.params.startTime.getTime();
      var dayCount = (endTime - startTime) / 1000 / 60 / 60 / 24;
      if (dayCount >= 60) {
        $scope.flag.arrow = false;
        CoreService.dialog('只能查询60天内的数据！');
        return;
      }
      var xAxisData = [];
      var seriesData = [];
      var option = {};
      var option1 = {};
      var option2 = {};

      $scope.send = srPerceptionRuqAnalysisService.fmtSendData($scope.params);
      var legendName = '成功率感知流失用户数量';
      var legendName1 = '成功率感知流失用户占比';
      var timeTitle = srPerceptionRuqAnalysisService.fmtDateTitle($scope.send);
      $scope.name = srPerceptionRuqAnalysisService.getName($scope.params.dimension);
      //标题:时间+各业务线成功率感知流失用户数量;
      $scope.title = timeTitle + ' ' + $scope.name + legendName;
      $scope.title1 = timeTitle + ' ' + $scope.name + legendName1;
      $scope.send.title = $scope.title;
      CoreLoading.setState(true);
      srPerceptionRuqAnalysisService.srPerceptionRuqAnalysisQuery($scope.send, function(res) {
        if (res.state === '10000' && res.data.length >= 1) {
          myEcharts.clear();
          myEcharts1.clear();
          myEcharts2.clear();
          angular.forEach(res.data, function(item) {
            if ($scope.params.dimension === 't') {
              xAxisData.push(item.time);
              seriesData.push(item.churnNumber);
            } else if ($scope.params.dimension === 'p') {
              xAxisData.push(item.provinceCodeName);
              seriesData.push(item.churnNumber);
            } else if ($scope.params.dimension === 'b') {
              xAxisData.push(item.businessLineName);
              seriesData.push(item.churnNumber);
            }
          });
          $scope.items = res.data;
          if ($scope.params.dimension === 't') {
            $scope.flag.showPie = false;
            $scope.showSR = $scope.params.dimension;
            option = srPerceptionRuqAnalysisService.getEchartsData(xAxisData, seriesData, $scope.title, legendName);
            option.title = {
              text: $scope.title,
              x: 'center'
            };
            option.tooltip.formatter = srPerceptionRuqAnalysisService.getFormatterFn('个');
            option.yAxis[0] = srPerceptionRuqAnalysisService.formatterYAxis('单位:(个)', '');
            myEcharts.setOption(option);
            $scope.outData = srPerceptionUwrAlSevice.exportView(myEcharts, $scope.send);
          } else {
            $scope.flag.showPie = true;
            $scope.showSR = $scope.params.dimension;
            option1 = srPerceptionUwrAlSevice.getEchartsData(xAxisData, seriesData, $scope.title, legendName);
            if ($scope.params.dimension === 'p') {
              option2 = srPerceptionUwrAlSevice.getEchartsPieData($scope.items, $scope.params.dimension, $scope.title1);
            } else {
              option2 = srPerceptionRuqAnalysisService.getEchartsPieData($scope.items, $scope.params.dimension, $scope.title1);
            }
            option1.tooltip.formatter = srPerceptionRuqAnalysisService.getFormatterFn('个');
            option1.yAxis[0] = srPerceptionRuqAnalysisService.formatterYAxis('单位:(个)', '');
            myEcharts1.setOption(option1);
            myEcharts2.setOption(option2);
            $scope.outData = srPerceptionRuqAnalysisService.exportView(myEcharts1, myEcharts2, $scope.send);
          }
          $scope.flag.state = false;
          $scope.flag.show = 'view';
          $scope.flag.arrow = false;
        } else if (res.data.length === 0) {
          $scope.flag.state = true;
          $scope.flag.arrow = true;
          $scope.flag.show = '';
          CoreService.dialog('查询无数据');
        } else {
          $scope.flag.state = true;
          $scope.flag.arrow = true;
          $scope.flag.show = '';
          CoreService.dialog('查询错误');
        }
        CoreLoading.setState(false);
      });
    };
    $scope.search();

  }
  srPerceptionRuqAnalysisCtrl.$inject = ['$scope', 'srPerceptionRuqAnalysisService', 'COREDATA', 'CoreLoading', 'srPerceptionUwrAlSevice', 'CoreService'];
})();